/*
 * MediaGridAdapter.java
 * classes : com.ruint.lib.mediachooser.adapter.MediaGridAdapter
 * @author ruint
 * V 1.0.0
 * Create at 2014-12-4 下午3:57:23
 */
package com.ruint.lib.mediachooser.adapter;

import java.io.File;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.ruint.core.adapter.BaseListAdapter;
import com.ruint.core.utils.ViewHolder;
import com.ruint.core.utils.bitmap.ImageLoaderUtils;
import com.ruint.corelib.R;
import com.ruint.lib.mediachooser.core.MediaChooserConstants;

/**
 * com.ruint.lib.mediachooser.adapter.MediaGridAdapter
 * 
 * @author ruint <br/>
 *         create at 2014-12-4 下午3:57:23
 */
public class MediaGridAdapter extends BaseListAdapter<String> {

  private int mWidth;

  private static final int VIEW_TYPE_COUNT = 2;

  public MediaGridAdapter(Context context) {
    super(context);
    this.mWidth = context.getResources().getDisplayMetrics().widthPixels;
  }

  @SuppressLint("NewApi")
  @SuppressWarnings("deprecation")
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
      convertView = LayoutInflater.from(context).inflate(R.layout.media_grid_item, parent, false);
    }
    ImageView icon = ViewHolder.get(convertView, R.id.image_grid_item);
    TextView info = ViewHolder.get(convertView, R.id.name_grid_item);
    LayoutParams imageParams = (LayoutParams) icon.getLayoutParams();
    imageParams.width = mWidth / 2;
    imageParams.height = mWidth / 2;
    icon.setLayoutParams(imageParams);
    if (position < MediaChooserConstants.MAX_MEDIA_LIMIT) {
      if (mList != null && mList.size() > 0) {
        File media = new File(mList.get(position));
        if (media.exists()) {
          if (media.getPath().contains("mp4") || media.getPath().contains("wmv") || media.getPath().contains("avi")
              || media.getPath().contains("3gp")) {
            icon.setImageBitmap(null);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
              icon.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.ic_video));
            } else {
              icon.setBackground(context.getResources().getDrawable(R.drawable.ic_video));
            }

          } else {
            ImageLoaderUtils.getInstance().displayImage(media.getAbsolutePath(), icon);
          }
          info.setText(media.getName());
        }
      } else {
        icon.setImageBitmap(null);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
          icon.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.add_photo));
        } else {
          icon.setBackground(context.getResources().getDrawable(R.drawable.add_photo));
        }
      }
    }
    return convertView;
  }

  @Override
  public int getCount() {
    if (mList != null && mList.size() > 0) {
      return mList.size() + 1;
    }
    return 1;
  }

  @Override
  public int getViewTypeCount() {
    return VIEW_TYPE_COUNT;
  }
}
